/* @formatter:off
 * Copyright (c) 2011 Lucas <lucas03093@gmail.com>
 * 
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
/* @formatter:on */

package nu.korkad.daytracker.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class PrefStore
{
    /** Current id */
    static final String  PREF_CURRENT_ID             = "id";
    
    /** Company name */
    static final String  PREF_COMPANY_NAME           = "companyName";
    
    /** Notification */
    static final String  PREF_NOTIFICATION           = "notification";
    
    /** Last update timestamp */
    static final String  PREF_LAST_UPDATE            = "lastupdate";
    
    /** Wifi only */
    static final String  PREF_WIFI_ONLY              = "wifionly";
    
    /** Crash reports */
    static final String  PREF_CRASH_REPORT           = "crashreport";
    
    /** Automatic updates */
    static final String  PREF_AUTO_UPDATE            = "autoupdate";
    
    /** Database version */
    static final String  PREF_DATABASE_VERSION       = "databaseversion";
    
    /** Dropbox key */
    static final String  PREF_DROPBOX_KEY            = "dropboxKey";
    
    /** Dropbox secret */
    static final String  PREF_DROPBOX_SECRET         = "dropboxSecret";
    
    /** Auto export */
    static final String  PREF_AUTO_EXPORT            = "autoexport";
    
    /** Device id */
    static final String  PREF_DEVICE_ID              = "deviceid";
    
    /** Default current id */
    static final int     DEFAULT_PREF_CURRENT_ID     = 0;
    
    /** Default company name */
    static final String  DEFAULT_PREF_COMPANY_NAME   = "";
    
    /** Default notification = true */
    static final boolean DEFAULT_PREF_NOTIFICATION   = true;
    
    /** Default last update timestamp = 0 */
    static final long    DEFAULT_PREF_LAST_UPDATE    = 0;
    
    /** Default wifi only = false */
    static final boolean DEFAULT_PREF_WIFI_ONLY      = false;
    
    /** Default crash report = false */
    static final boolean DEFAULT_PREF_CRASH_REPORT   = false;
    
    /** Default automatic updates = true */
    static final boolean DEFAULT_PREF_AUTO_UPDATE    = true;
    
    /** Default database version */
    static final int     DEFAULT_DATABASE_VERSION    = 0;
    
    /** Default dropbox key */
    static final String  DEFAULT_PREF_DROPBOX_KEY    = "";
    
    /** Default dropbox secret */
    static final String  DEFAULT_PREF_DROPBOX_SECRET = "";
    
    /** Default auto export */
    static final boolean DEFAULT_PREF_AUTO_EXPORT    = false;
    
    /** Default device id */
    static final String  DEFAULT_PREF_DEVICE_ID      = "";
    
    // General preferences
    static SharedPreferences getPrefs(Context context)
    {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }
    
    // Private preferences
    static SharedPreferences getPrivPrefs(Context context)
    {
        return context.getSharedPreferences(Config.PREFSTORE_PRIVATE, Context.MODE_PRIVATE);
    }
    
    // Dropbox preferences
    static SharedPreferences getDropboxPrefs(Context context)
    {
        return context.getSharedPreferences(Config.PREFSTORE_DROPBOX, Context.MODE_PRIVATE);
    }
    
    // Get old values
    public static int getOldCurrentId(Context context)
    {
        return getPrivPrefs(context).getInt(PREF_CURRENT_ID, DEFAULT_PREF_CURRENT_ID);
    }
    
    public static String getOldCompanyName(Context context)
    {
        return getPrivPrefs(context).getString(PREF_COMPANY_NAME, DEFAULT_PREF_COMPANY_NAME);
    }
    
    public static int getOldDatabaseVersion(Context context)
    {
        return getPrivPrefs(context).getInt(PREF_DATABASE_VERSION, DEFAULT_DATABASE_VERSION);
    }
    
    /**
     * Get current id.
     * 
     * @param context
     * @return int
     * @see PrefStore#DEFAULT_PREF_CURRENT_ID
     */
    public static int getCurrentId(Context context)
    {
        return getPrefs(context).getInt(PREF_CURRENT_ID, DEFAULT_PREF_CURRENT_ID);
    }
    
    /**
     * Set current id.
     * 
     * @param context
     * @param currentId
     *            long
     */
    public static void setCurrentId(Context context, int currentId)
    {
        getPrefs(context).edit().putInt(PREF_CURRENT_ID, currentId).commit();
    }
    
    /**
     * Get company name.
     * 
     * @param context
     * @return String
     */
    public static String getCompanyName(Context context)
    {
        return getPrefs(context).getString(PREF_COMPANY_NAME, DEFAULT_PREF_COMPANY_NAME);
    }
    
    /**
     * Set company name.
     * 
     * @param context
     * @param company
     *            String
     */
    public static void setCompanyName(Context context, String company)
    {
        getPrefs(context).edit().putString(PREF_COMPANY_NAME, company).commit();
    }
    
    /**
     * Returns true if notification should be enabled.
     * 
     * @param context
     * @return boolean
     * @see PrefStore#DEFAULT_PREF_VIBRATION
     */
    public static boolean getNotification(Context context)
    {
        return getPrefs(context).getBoolean(PREF_NOTIFICATION, DEFAULT_PREF_NOTIFICATION);
    }
    
    /**
     * Set notification.
     * 
     * @param context
     * @param notification
     *            boolean
     */
    public static void setNotification(Context context, boolean notification)
    {
        getPrefs(context).edit().putBoolean(PREF_NOTIFICATION, notification).commit();
    }
    
    /**
     * Get last update timestamp.
     * 
     * @param context
     * @return long
     * @see PrefStore#DEFAULT_PREF_LAST_UPDATE
     */
    public static long getLastUpdate(Context context)
    {
        return getPrefs(context).getLong(PREF_LAST_UPDATE, DEFAULT_PREF_LAST_UPDATE);
    }
    
    /**
     * Set last update timestamp.
     * 
     * @param context
     * @param lastupdate
     *            long
     */
    public static void setLastUpdate(Context context, long lastupdate)
    {
        getPrefs(context).edit().putLong(PREF_LAST_UPDATE, lastupdate).commit();
    }
    
    /**
     * Get WiFi only.
     * 
     * @param context
     * @return boolean
     * @see PrefStore#DEFAULT_PREF_WIFI_ONLY
     */
    public static boolean getWifiOnly(Context context)
    {
        return getPrefs(context).getBoolean(PREF_WIFI_ONLY, DEFAULT_PREF_WIFI_ONLY);
    }
    
    /**
     * Set WiFi only.
     * 
     * @param context
     * @param wifionly
     *            boolean
     */
    public static void setWifiOnly(Context context, boolean wifionly)
    {
        getPrefs(context).edit().putBoolean(PREF_WIFI_ONLY, wifionly).commit();
    }
    
    /**
     * Get crash report.
     * 
     * @param context
     * @return boolean
     * @see PrefStore#DEFAULT_PREF_CRASH_REPORT
     */
    public static boolean getCrashReport(Context context)
    {
        return getPrefs(context).getBoolean(PREF_CRASH_REPORT, DEFAULT_PREF_CRASH_REPORT);
    }
    
    /**
     * Set crash report.
     * 
     * @param context
     * @param crashreport
     *            boolean
     */
    public static void setCrashReport(Context context, boolean crashreport)
    {
        getPrefs(context).edit().putBoolean(PREF_CRASH_REPORT, crashreport).commit();
    }
    
    /**
     * Get auto update.
     * 
     * @param context
     * @return boolean
     * @see PrefStore#DEFAULT_PREF_AUTO_UPDATE
     */
    public static boolean getAutoUpdate(Context context)
    {
        return getPrefs(context).getBoolean(PREF_AUTO_UPDATE, DEFAULT_PREF_AUTO_UPDATE);
    }
    
    /**
     * Set auto update.
     * 
     * @param context
     * @param autoupdate
     *            boolean
     */
    public static void setAutoUpdate(Context context, boolean autoupdate)
    {
        getPrefs(context).edit().putBoolean(PREF_AUTO_UPDATE, autoupdate).commit();
    }
    
    /**
     * Get database version.
     * 
     * @param context
     * @return int
     */
    public static int getDatabaseVersion(Context context)
    {
        return getPrefs(context).getInt(PREF_DATABASE_VERSION, DEFAULT_DATABASE_VERSION);
    }
    
    /**
     * Set database version.
     * 
     * @param context
     * @param databaseVersion
     *            int
     */
    public static void setDatabaseVersion(Context context, int databaseVersion)
    {
        getPrefs(context).edit().putInt(PREF_DATABASE_VERSION, databaseVersion).commit();
    }
    
    /**
     * Get dropbox key.
     * 
     * @param context
     * @return String
     */
    public static String getDropboxKey(Context context)
    {
        return getDropboxPrefs(context).getString(PREF_DROPBOX_KEY, DEFAULT_PREF_DROPBOX_KEY);
    }
    
    /**
     * Set dropbox key.
     * 
     * @param context
     * @param key
     *            String
     */
    public static void setDropboxKey(Context context, String key)
    {
        getDropboxPrefs(context).edit().putString(PREF_DROPBOX_KEY, key).commit();
    }
    
    /**
     * Get dropbox secret.
     * 
     * @param context
     * @return String
     */
    public static String getDropboxSecret(Context context)
    {
        return getDropboxPrefs(context).getString(PREF_DROPBOX_SECRET, DEFAULT_PREF_DROPBOX_SECRET);
    }
    
    /**
     * Set dropbox secret.
     * 
     * @param context
     * @param secret
     *            String
     */
    public static void setDropboxSecret(Context context, String secret)
    {
        getDropboxPrefs(context).edit().putString(PREF_DROPBOX_SECRET, secret).commit();
    }
    
    /**
     * Get auto export.
     * 
     * @param context
     * @return boolean
     */
    public static boolean getAutoExport(Context context)
    {
        return getPrefs(context).getBoolean(PREF_AUTO_EXPORT, DEFAULT_PREF_AUTO_EXPORT);
    }
    
    /**
     * Set auto export.
     * 
     * @param context
     * @param autoexport
     *            boolean
     */
    public static void setAutoExport(Context context, boolean autoexport)
    {
        getPrefs(context).edit().putBoolean(PREF_AUTO_EXPORT, autoexport).commit();
    }
    
    /**
     * Get device id.
     * 
     * @param context
     * @return String
     */
    public static String getDeviceId(Context context)
    {
        return getPrefs(context).getString(PREF_DEVICE_ID, DEFAULT_PREF_DEVICE_ID);
    }
    
    /**
     * Set device id
     * 
     * @param context
     * @param deviceid
     *            String
     */
    public static void setDeviceId(Context context, String deviceid)
    {
        getPrefs(context).edit().putString(PREF_DEVICE_ID, deviceid).commit();
    }
}
